#coding=utf-8
import json
import threading

import pythoncom
import wmi


class HotfixDetect(threading.Thread):
    """
    用于探测补丁的类
    """
    def __init__(self,mq,data):
        super().__init__()
        #传递过来的MQ对象
        self.__mq=mq
        #传递过来的数据
        self.__data=data

    def run(self):
        self.__hotfix_discovery()

    def __hotfix_discovery(self):
        """
        补丁发现
        :return:
        """
        print("开始补丁安全发现......!")
        # 初始化
        pythoncom.CoInitialize()
        # 创建WMI客户端
        c = wmi.WMI()
        # 获取补丁信息
        hotfixes = c.query("SELECT HotFixID FROM Win32_QuickFixEngineering")
        # 组装补丁ID
        hotfix_list = []
        for hotfix in hotfixes:
            data = {
                'macAddress': self.__data['macAddress'],
                'hotfixId': hotfix.HotFixID
            }
            hotfix_list.append(data)
        # 去初始化
        pythoncom.CoUninitialize()
        # 转换成JSON数据
        data = json.dumps(hotfix_list)
        # 发送到队列
        self.__mq.produce_hotfix_info(data)
        print("补丁安全发现结束")
